<?php 
namespace app\index\controller;
use app\index\model\Msg;
use \think\Controller;
use \think\Request;
use \think\Loader;

class User extends Common
{
    public function index()
    {
        $user = new \app\index\model\User();
        $user = $user->find(input('uid'));
        $question = db('question')
            ->field('qid,title,answer_num,view_num,ctime')
            ->where('uid',input('uid'))
            ->order('ctime desc')
            ->select();
        $answer = db('answer')
            ->alias('a')
            ->join('question q','a.qid = q.qid')
            ->where('a.uid',input('uid'))
            ->field('q.qid,q.title,a.content,a.ctime')
            ->order('ctime desc')
            ->select();
        return $this->fetch('',[
            'title'=>'用户主页',
            'user' => $user,
            'question'=>$question,
            'answer' => $answer,
        ]);
    }
    public function set()
    {
        $user = new \app\index\model\User();
        $res = $user->find(session('uid'));
        return $this->fetch('', [
            'title' =>'用户设置',
            'res' => $res,
            ]);
    }
    public function checkFace()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>156780,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $data['face'] = '/uploads/'.$info->getSaveName();
            $data['uid'] = session('uid');
            $res = db('user')->update($data);
            if($res){
                session('face',$data['face']);
                echo json_encode(['error'=>0,'info'=>'上传成功']);
            }else{
                echo json_encode(['error'=>0,'info'=>'上传失败']);
            }

        } else {
            // 上传失败获取错误信息
            echo json_encode(['error'=>0,'info'=>$file->getError()]);
        }
    }

    public function checkSet()
    {
        $data = Request::instance()->param();
        foreach ($data as $k=>$v){
            if (empty($v)){
                unset($data[$k]);
            }
        }
        $validate = Loader::validate('User');
        if(!$validate->scene('edit')->check($data)){
            exit(json_encode(['error'=>1,'info'=>$validate->getError()]));
        }
        $user = new \app\index\model\User();
        $user->updateUser($data);
    }

    public function msg()
    {
        $msg = new Msg();
        $msg = $msg ->getMsg();
        return $this->fetch('',[
            'title' => '我的消息',
            'msg' => $msg,
        ]);
    }
    public function myArt()
    {
        $questionsNum = db('question')->where('uid',session('uid'))->count();
        $shoucang = db('collect')->where('uid',session('uid'))->count();
        $questions = db('question')->field('qid,title,ctime')->where('uid',session('uid'))->paginate(5);
        $collect = db('collect')
            ->alias('c')
            ->join('question q','c.qid = q.qid','RIGHT')
            ->field('q.qid,q.title,c.ctime')
            ->order('q.ctime desc')
            ->where('c.uid',session('uid'))
            ->paginate(5);
        return $this->fetch('',[
            'title'=>'我的信息',
            'qnum' =>$questionsNum,
            'questions' => $questions,
            'shoucang' => $shoucang,
            'collect' => $collect
        ]);
    }

}
